// component/add_address/add_address.js
const app = getApp();
const api = require("../../utils/api.js");
const urls = require("../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */

  data: {
    name: '',
    phoneNumber: '',
    region: '请输入地址',
    hiddenRegionName: '',
    regionDetail: '',
    isSwitch: false,
    defaultPId: {
      parentId: 1
    },
    defaultCId: {
      parentId: 2
    },
    defaultAId: {
      parentId: 37
    },
    provinceName: [],
    provinceId: [],
    cityName: [], //由各个省的下级市数组组成的数组
    cityId: [],
    areaName: [], //由县组成小数组再组成市的数组再组成省的数组
    areaId: [],
    regionNameArr: [],
    regionId: [],
  },
  //name输入失去焦点时触发
  nameFun(e) {
    this.setData({
      name: e.detail.value,
    })
  },
  //phoneNumber输入框失去焦点时触发
  phoneFun(e) {
    this.setData({
      phoneNumber: e.detail.value,
    })
  },
  //详细地址文本框失去焦点时触发
  regionDetailFun(e) {
    this.setData({
      regionDetail: e.detail.value,
    })
  },
  //设置为默认地址时触发
  switchChange(e) {
    this.setData({
      isSwitch: e.detail.value,
    })
  },
  //保存时触发
  confirmAddress() {
    let that=this;
    // console.log(that.data.name, that.data.phoneNumber, that.data.provinceName[that.data.regionId[0]], that.data.cityId[that.data.regionId[1]], that.data.areaId[that.data.regionId[2]], that.data.regionDetail, that.data.isSwitch)
    urls.merReq(api.updateAddressUrl, {
      id:'',
      name: that.data.name,
      mobile: that.data.phoneNumber,
      province_id: that.data.provinceId[that.data.regionId[0]],
      city_id: that.data.cityId[that.data.regionId[1]],
      district_id: that.data.areaId[that.data.regionId[2]],
      address: that.data.regionDetail,
      is_default: that.data.isSwitch
    }, res => {
      console.log(123,res)
      }, 'POST')
  },
  //确定时触发的事件
  onRegionConfirm() {
    this.setData({
      region: this.data.hiddenRegionName,
    })
  },
  //省市区改变时触发的事件
  onRegionChange(e) {
    let that = this;
    if (e.detail.column == 0) {
      urls.merReq(api.regionUrl, {
        parentId: this.data.provinceId[e.detail.value]
      }, res => {
        var cityNameArr = [],
          cityIdArr = [];
        res.data.data.map(function(val, index) {
          cityNameArr.push(val.name);
          cityIdArr.push(val.id);
        })
        that.setData({
          cityName: cityNameArr,
          cityId: cityIdArr,
          ['regionNameArr[1]']: cityNameArr,
        })
        urls.merReq(api.regionUrl, {
          parentId: cityIdArr[0]
        }, res => {
          var areaNameArr = [],
            areaIdArr = [];
          res.data.data.map(function(val, index) {
            areaNameArr.push(val.name);
            areaIdArr.push(val.id);
          })
          that.setData({
            areaName: areaNameArr,
            areaId: areaIdArr,
            ['regionNameArr[2]']: areaNameArr,
            regionId: [e.detail.value, 0, 0],
            hiddenRegionName: this.data.provinceName[e.detail.value] + '-' + cityNameArr[0] + '-' + areaNameArr[0],
          })
        }, 'GET')
        }, 'GET')
    } else if (e.detail.column == 1) {
      urls.merReq(api.regionUrl, {
        parentId: this.data.cityId[e.detail.value]
      }, 'GET', res => {
        var areaNameArr = [],
          areaIdArr = [];
        res.data.data.map(function(val, index) {
          areaNameArr.push(val.name);
          areaIdArr.push(val.id);
        })
        that.setData({
          areaName: areaNameArr,
          areaId: areaIdArr,
          ['regionNameArr[2]']: areaNameArr,
          ['regionId[1]']: e.detail.value,
          ['regionId[2]']: 0,
          hiddenRegionName: this.data.provinceName[this.data.regionId[0]] + '-' + this.data.cityName[e.detail.value] + '-' + areaNameArr[0],
        })
      })
    } else {
      that.setData({
        ['regionId[2]']: e.detail.value,
        hiddenRegionName: this.data.provinceName[this.data.regionId[0]] + '-' + this.data.cityName[this.data.regionId[1]] + '-' + this.data.areaName[e.detail.value]
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    //请求省的数据
    urls.merReq(api.regionUrl, this.data.defaultPId, res => {
      var provinceNameArr = [],
        provinceIdArr = [];
      res.data.data.map(function(val, index) {
        provinceNameArr.push(val.name);
        provinceIdArr.push(val.id);
      })
      urls.merReq(api.regionUrl, this.data.defaultCId,  res => {
        var cityNameArr = [],
          cityIdArr = [];
        res.data.data.map(function(val, index) {
          cityNameArr.push(val.name);
          cityIdArr.push(val.id);
        })
        urls.merReq(api.regionUrl, this.data.defaultAId,  res => {
          var areaNameArr = [],
            areaIdArr = [];
          res.data.data.map(function(val, index) {
            areaNameArr.push(val.name);
            areaIdArr.push(val.id);
          })
          that.setData({
            provinceName: provinceNameArr,
            provinceId: provinceIdArr,
            cityName: cityNameArr,
            cityId: cityIdArr,
            areaName: areaNameArr,
            areaId: areaIdArr,
            regionNameArr: [provinceNameArr, cityNameArr, areaNameArr],
          })
        }, 'GET')
      }, 'GET')
    }, 'GET')










    //请求省的数据
    // urls.req(api.regionUrl, this.data.defaultPId, 'GET', res => {
    //   var provinceNameArr = [], //用来装省名
    //     provinceIdArr = []; //用来装省id
    //   var cityNameArr = [], //用来装(市名组成的数组)
    //     cityIdArr = []; //用来装(市id组成的数组)
    //   var areaNameArr = [], //用来装 [(地区名组成的数组)组成的数组]
    //     areaIdArr = []; //用来装 [(地区id组成的数组)组成的数组]
    //     var count=0;
    //   //遍历省请求结果
    //   res.data.data.map(function(val, index) {
    //     provinceNameArr.push(val.name); //将省名装入provinceNameArr
    //     provinceIdArr.push(val.id); //将省id装入provinceIdArr
    //   });
    //   var citynameArr = [], //用来装(市名组成的数组)
    //     cityidArr = []; //用来装(市id组成的数组)
    //   //遍历省id
    //   provinceIdArr.map(function(val, index) {
    //     //请求市数据
    //     urls.req(api.regionUrl, {
    //       parentId: val
    //     }, 'GET', res => {
    //       var areanameArr = [], //用来装[(区县名组成的数组)组成的数组]
    //         areaidArr = []; //用来装[(区县id组成的数组)组成的数组]
    //       //遍历请求结果
    //       res.data.data.map(function(val, index) {
    //         citynameArr.push(val.name); //将请求到的市名装入citynameArr
    //         cityidArr.push(val.id); //将请求到的市id装入cityidArr
    //       })
    //       cityNameArr.push(citynameArr); //将citynameArr装入cityNameArr
    //       cityIdArr.push(cityidArr); //将cityidArr装入cityIdArr
    //       //遍历请求到的市id数组,请求每一个市下面的区县
    //       cityidArr.map(function(val, index) {
    //         //请求区县数据
    //         urls.req(api.regionUrl, {
    //           parentId: val
    //         }, 'GET', res => {
    //           var areanamearr = [], //用来装区县名
    //             areaidarr = []; //用来装区县id
    //           res.data.data.map(function(val, index) {
    //             areanamearr.push(val.name); //把请求到的区县名装入数组areanamearr
    //             areaidarr.push(val.id); //把请求到的区县id装入数组areaidarr
    //           })
    //           areanameArr.push(areanamearr); //把areanamearr装入areanameArr
    //           areaidArr.push(areaidarr); //将areaidarr装入areaidArr
    //         })
    //       })
    //       areaNameArr.push(areanameArr); //将areanameArr装入areaNameArr
    //     })
    //   })
    //   this.setData({
    //     provinceName: provinceNameArr,
    //     provinceId: provinceIdArr,
    //     cityName: cityNameArr,
    //     cityId: cityIdArr,
    //     areaName: areaNameArr,
    //   })
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})